package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.newEntity.UniAppMenu;
import com.tbit.uqbike.client.service.UniAppMenuService;
import com.tbit.uqbike.client.util.Assert;
import com.tbit.uqbike.client.util.OutputUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 小程序自定义界面
 *
 * @Author: chen
 * @Description:
 * @Date: 创建于 15:37 2022/9/21
 */
@Controller
@RequestMapping("/menu")
public class UniAppMenuController {

    @Autowired
    private UniAppMenuService uniAppMenuService;

    /**
     * 根据小程序版本号查询菜单
     *
     * @param adAccountId 区域id
     * @param request
     * @param response
     * @param session
     */
    @RequestMapping("/query")
    public void query(Integer adAccountId,HttpServletRequest request, HttpServletResponse response,
                      HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        try {
            Assert.notNull(adAccountId, "-10001", "区域id不能为空");
            String version = request.getHeader("U-App-VERSION");
            if (Objects.isNull(version)){
                version = request.getHeader("T-App-Version");
            }
            List<UniAppMenu> uniAppMenus = uniAppMenuService.getByAccountId(adAccountId,version);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, uniAppMenus);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }
}
